package grsu.epam.airline.action;

import java.util.HashMap;
import java.util.Map;

import grsu.epam.airline.form.FlightModifyForm;
import grsu.epam.airline.service.FlightCrewService;
import grsu.epam.airline.service.FlightService;
import grsu.epam.airline.service.PlaneService;
import grsu.epam.airline.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.LookupDispatchAction;

public class FlightAction extends LookupDispatchAction {
	String target;

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			FlightCrewService flightCrewService = new FlightCrewService();
			request.setAttribute("flightCrews",
					flightCrewService.getAllFlightCrew());
			PlaneService planeService = new PlaneService();
			request.setAttribute("planes", planeService.getAllPlane());
			target = ForwardActionTo.ADDFLIGHT;
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward route(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			FlightModifyForm input = (FlightModifyForm) form;
			FlightService flightService = new FlightService();
			if (input.getFlightId() != 0) {
				request.setAttribute("flight",
						flightService.getFlightById(input.getFlightId()));
				target = ForwardActionTo.ROUTE;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseFlight", new ActionMessage("error.flight"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
				request.setAttribute("flights", flightService.getAllFlight());
			}
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			FlightModifyForm input = (FlightModifyForm) form;
			FlightService flightService = new FlightService();
			if (input.getFlightId() != 0) {
				flightService.deleteFlight(input.getFlightId());
				target = ForwardActionTo.FLIGHTS;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseFlight", new ActionMessage("error.flight"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
			}
			request.setAttribute("flights", flightService.getAllFlight());
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);

	}

	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			FlightModifyForm input = (FlightModifyForm) form;
			FlightService flightService = new FlightService();
			if (input.getFlightId() != 0) {
				request.setAttribute("flight",
						flightService.getFlightById(input.getFlightId()));
				FlightCrewService flightCrewService = new FlightCrewService();
				request.setAttribute("flightCrews",
						flightCrewService.getAllFlightCrew());
				PlaneService planeService = new PlaneService();
				request.setAttribute("planes", planeService.getAllPlane());
				target = ForwardActionTo.UPDATEFLIGHT;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseFlight", new ActionMessage("error.flight"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
				request.setAttribute("flights", flightService.getAllFlight());
			}
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);

	}

	@Override
	protected Map getKeyMethodMap() {
		Map map = new HashMap();
		map.put("flight.add", "add");
		map.put("flight.delete", "delete");
		map.put("flight.modify", "modify");
		map.put("flight.route", "route");
		return map;
	}
}